//
//  RegisterViewController.m
//  LeagueOfLegend
//
//  Created by appying_wxj on 14-7-14.
//  Copyright (c) 2014年 appying_wxj. All rights reserved.
//

#import "RegisterViewController.h"
#import <AVOSCloud/AVOSCloud.h>
//#import <AdSupport/ASIdentifierManager.h>
#import "MobClick.h"


#define Kwidth self.view.frame.size.width

#define Kheight self.view.frame.size.height
@interface RegisterViewController ()

@end

@implementation RegisterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UIView *viewBG = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Kwidth, Kheight)];
    viewBG.backgroundColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1];
    [self.view addSubview:viewBG];
    
    [self.navigationController setNavigationBarHidden:YES];
//    self.view.backgroundColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    backBtn.backgroundColor = [UIColor clearColor];
    [backBtn setTitle:@"<" forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backAgreeViewButton:) forControlEvents:UIControlEventTouchUpInside];
    [viewBG addSubview:backBtn];
    
    // 上面的白色框
    _subView = [[UIView alloc] init];
    _subView.backgroundColor = [UIColor whiteColor];
    [_subView.layer setCornerRadius:10];
    [viewBG addSubview:_subView];
    
    _nickName = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 220, 40)];
    _nickName.backgroundColor = [UIColor whiteColor];
    _nickName.layer.borderWidth = 1;
    _nickName.layer.borderColor = [UIColor colorWithRed:0.87 green:0.87 blue:0.87 alpha:1].CGColor;
    [_nickName.layer setCornerRadius:10];
    [_subView addSubview:_nickName];

    //确定按钮
    UIButton *ensureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    ensureBtn.frame = CGRectMake(240, 10, 40, 40);
    [ensureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [ensureBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [ensureBtn addTarget:self action:@selector(ensureButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [_subView addSubview:ensureBtn];
    
    if (version>=7) {
        _subView.frame = CGRectMake(10, (Kheight - 49 - 20 - 60)/2, 320 - 20, 60);
        backBtn.frame = CGRectMake(10, 30, 20, 20);
    }else{
        _subView.frame = CGRectMake(10, (Kheight - 49 - 20 - 60)/2, 320 - 20, 60);
        backBtn.frame = CGRectMake(10, 20, 20, 20);
    }
   
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"RegisterViewController"];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"RegisterViewController"];
}


#pragma mark----buttonClick
// 返回按钮
-(void)backAgreeViewButton:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
    [MobClick event:@"RegisterVCBackButton"];
}
// 注册昵称的确定按钮
-(void)ensureButtonClick:(UIButton *)button
{
        if (_nickName.text.length < 2) {
        UIAlertView *alrtView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"昵称不得少于2个字符" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        alrtView.delegate = self;
        alrtView.tag = 18890;
        [alrtView show];
        }else  if(_nickName.text.length > 8){
            UIAlertView *alrtView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"昵称不得超过8个字符" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            alrtView.tag = 18889;
            alrtView.delegate = self;
            [alrtView show];
        }else{
//            NSString *adId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
//            // 获取关联数据
//            AVQuery *query = [[AVQuery alloc]initWithClassName:@"equipment"];
//            [query whereKey:@"adId" equalTo:adId];
//            NSArray *equipmentArray = [query findObjects];
//            //判断设备号是否存在，不存在则新建
//            if (equipmentArray.count == 0) {
//                AVObject *equipment = [AVObject objectWithClassName:@"equipment"];
//                [equipment setObject:adId forKey:@"adId"];
//                [equipment save];
//            }else {
//                NSString *objectID = [[equipmentArray objectAtIndex:0] objectId];
//                AVQuery *query = [[AVQuery alloc]initWithClassName:@"equipment"];
//                [query whereKey:@"adId" equalTo:adId];
//                AVObject *object =[query getObjectWithId:objectID];
//                [object save];
//            }
            
            
            NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
            NSString *name =_nickName.text;
            [defaults setObject:name forKey:@"name"];
            [defaults synchronize]; // 同步   保持两边的数据一致
            
            UIAlertView *shonicknamewSuccess = [[UIAlertView alloc] initWithTitle:nil message:@"昵称建立成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            shonicknamewSuccess.delegate = self;
            shonicknamewSuccess.tag = 18888;
            [shonicknamewSuccess show];
        }
    [MobClick event:@"RegisterVCSure"];
}
#pragma mark - alertView delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==18888) {
        [self dismissViewControllerAnimated:YES completion:Nil];
        
    }
    NSLog(@"-------");
    if (buttonIndex ==0) {
        
        return;
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
